home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / umich / telecomm / fnordadl / fn132src.zoo / citutil / loadnet.c < prev    next >
C/C++ Source or Header  |  1991-09-02  |  6KB  |  287 lines

  1. /*
  2.  * loadnet.c - convert the output of dumpnet back into a netTab[]
  3.  *
  4.  * 88Aug07 orc    restore external dialer setting, lastmessage settings
  5.  * 88Jul30 orc    add -NNN flag for setting lastmessage
  6.  * 88Apr27 orc    created.
  7.  */
  8.  
  9. #include "ctdl.h"
  10. #include "net.h"
  11. #include "room.h"
  12. #include "config.h"
  13. #include "event.h"
  14. #include "citlib.h"
  15.  
  16. char *program = "loadnet";
  17. long lastmessage;
  18. #if 0
  19. struct logBuffer logBuf;    /* MENTAL! */    /* ????????????? */
  20. #endif
  21.  
  22. void plusnode();
  23.  
  24. int
  25. getaline(char *s, FILE *fd)
  26. {
  27.     int rc;
  28.     int idx;
  29.  
  30.     for (idx=0;idx<80;idx++)
  31.     s[idx] = 0;
  32.     rc = fgets(s,80,fd) ? 1 : 0;
  33.     s[idx=79] = 0;
  34.     while (s[idx] == 0 || s[idx] == '\r' || s[idx] == '\n') {
  35.     s[idx--] = 0;
  36.     if (idx < 0)
  37.         break;
  38.     }
  39.     return rc;
  40. }
  41.  
  42. int
  43. normID(LABEL source, LABEL dest)
  44. {
  45.     while (!isalpha(*source) && *source)
  46.     source++;
  47.     if (!*source) return NO;
  48.     *dest++ = toupper(*source++);
  49.     while (!isalpha(*source) && *source)
  50.     source++;
  51.     if (!*source) return NO;
  52.     *dest++ = toupper(*source++);
  53.     while (*source) {
  54.     if (isdigit(*source))
  55.         *dest++ = *source;
  56.     source++;
  57.     }
  58.     *dest = 0;
  59.     return YES;
  60. }
  61.  
  62. void
  63. plusnode(char *line0, FILE *fd)
  64. {
  65.     char *node, *id, *p, *strchr();
  66.     static char line[81];
  67.     long lastin, atol();
  68.     int i, netidhash;
  69.     int rmno;
  70.     LABEL temp;
  71.  
  72.     /* Following replaces a zero_struct(netBuf); AA 90Jan31 */
  73.     memset(&netBuf, 0, NB_SIZE);
  74.     memset(netBuf.shared, 0, SR_BULK);
  75.  
  76.     strtok(line0," ");
  77.     node = strtok(NULL,"@");
  78.     id   = strtok(NULL,"\0");
  79.     if (node && id && node[0] && id[0]) {
  80.     if (!NNisok(node))
  81.         crashout("bad nodename <%s>", node);
  82.     if (netnmidx(node) >= 0)
  83.         crashout("duplicate node name <%s>", node);
  84.     normID(id, temp);        /* replaces normID() call below */
  85.     netidhash = hash(temp);
  86. #if 0
  87.     for (i=0; i < cfg.netSize; i++)
  88.         if (netidhash == netTab[i].ntidhash)
  89.         crashout("duplicate node ID <%s> (conflict: slot %d)", id, i);
  90. #endif
  91.     }
  92.     else
  93.     crashout("Bad nodename/node id");
  94.  
  95.     strcpy(netBuf.netName, node);
  96.     strcpy(netBuf.netId,   id);
  97.     set(netBuf,N_INUSE);
  98.  
  99.     printf("creating node %s @ %s\n", node, id);
  100.  
  101.     getaline(line,fd);
  102.     if (strncmp(line, "mypw", 4) != 0)
  103.     crashout("bad mypw line (%s) in node %s", line, node);
  104.     p = strtok(line, " ");
  105.     p = strtok(NULL, "\0");
  106.     if (p)
  107.     strcpy(netBuf.myPasswd, p);
  108.  
  109.     getaline(line,fd);
  110.     if (strncmp(line, "herpw", 5) != 0)
  111.     crashout("bad herpw line (%s) in node %s", line, node);
  112.     p = strtok(line, " ");
  113.     p = strtok(NULL, "\0");
  114.     if (p)
  115.     strcpy(netBuf.herPasswd, p);
  116.         
  117.     getaline(line,fd);
  118.     sscanf(line, "%lx", &netBuf.flags);
  119.  
  120.     getaline(line,fd);
  121.     netBuf.baudCode = line[0]-'0';
  122.     for (p=strtok(1+line,","); p; p=strtok(NULL,","))
  123.     switch (p[0]) {
  124.     case 'L':
  125.         netBuf.ld = atoi(1+p);
  126.         break;
  127.     case 'D':
  128.         netBuf.dialer = atoi(1+p);
  129.         break;
  130.     }
  131.  
  132.     getaline(line,fd);
  133.     if (strncmp(line, "access", 6) != 0)
  134.     crashout("bad access line (%s) in node %s", line, node);
  135.     p = strtok(line, " ");
  136.     p = strtok(NULL, "\0");
  137.     if (p)
  138.     strcpy(netBuf.access, p);
  139.         
  140.     getaline(line,fd);
  141.     sscanf(line, "%lx,%x",&netBuf.what_net, &netBuf.poll_day);
  142.  
  143.     netTab = realloc(netTab, (++cfg.netSize) * sizeof(*netTab));
  144.     netTab[cfg.netSize-1].Tshared = (struct netroom *) xmalloc(SR_BULK);
  145.     memset(netTab[cfg.netSize-1].Tshared, 0, SR_BULK);    /* AA 91Jan21 */
  146.  
  147.     /* normID() and hash() moved up a ways */
  148.     netTab[cfg.netSize-1].ntnmhash = hash(netBuf.netName);
  149.     netTab[cfg.netSize-1].ntidhash = netidhash;
  150.     for (i=0; i<SHARED_ROOMS; i++)
  151.     netBuf.shared[i].NRidx = -1;
  152.     
  153.     for (i=0; getaline(line,fd); i++) {
  154.     if ((strlen(line) < 1) || (strncmp(line, "#node", 5) == 0))
  155.         break;                    /* Sanity check */
  156.     if (p=strchr(line, '\t')) {
  157.         *p++ = 0;
  158.         while (*p == ' ' || *p == '\t')
  159.         ++p;
  160.         lastin = atol(p);
  161.         if (lastin > cfg.newest)
  162.         lastin = cfg.newest;
  163.     }
  164.     else lastin = lastmessage;
  165.     
  166.     if (i < SHARED_ROOMS) {
  167.         printf("sharing %s\n", line);
  168.         if ((rmno = roomExists(line)) >= 0) {
  169.         netBuf.shared[i].NRidx = rmno;
  170.         netBuf.shared[i].NRlast= lastin;
  171.         netBuf.shared[i].NRgen = roomTab[rmno].rtgen;
  172.         netBuf.shared[i].NRhub = netBuf.ld ? 1 : 0;
  173.         if (!readbit(roomTab[rmno],SHARED)) {
  174.             getRoom(rmno);
  175.             set(roomBuf,SHARED);
  176.             noteRoom();
  177.             putRoom(rmno);
  178.         }
  179.         }
  180.         else
  181.         fprintf(stderr, "room %s does not exist!\n", line);
  182.     }
  183.     }
  184.     putNet(cfg.netSize-1);
  185. }
  186.  
  187. /*
  188.  * writeNet(): write up nodes on the net.  Format:
  189.  *
  190.  *    #node name@id
  191.  *    mypw myPasswd
  192.  *    herpw herPasswd
  193.  *    flags
  194.  *    baud,dialer,ld
  195.  *    access
  196.  *    whatnet,poll-days
  197.  *    [rooms shared]
  198.  *        .
  199.  *        .
  200.  *        .
  201.  *    <blank line>
  202.  */
  203. void
  204. loadnodes(FILE *fd)
  205. {
  206.     static char line[80];
  207.  
  208.     while (getaline(line,fd)) {
  209.     if (strncmp(line,"#node",5) != 0)
  210.         continue;
  211.     plusnode(line, fd);
  212.     }
  213.     printf("loaded %d node%s\n", cfg.netSize, (cfg.netSize != 1)?"s":"");
  214. }
  215.  
  216. /*
  217.  * roomExists(): returns slot# of named room
  218.  */
  219. int
  220. roomExists(char *room)
  221. {
  222.     int i;
  223.  
  224.     for (i = 0; i < MAXROOMS; i++)
  225.     if (readbit(roomTab[i],INUSE) && stricmp(room, roomTab[i].rtname) == 0)
  226.         return i;
  227.     return ERROR;
  228. }
  229.  
  230. main(int argc, char **argv)
  231. {
  232.     static char temp[200];
  233.     char c;
  234.     FILE *listing, *fopen();
  235.     long atol();
  236.  
  237.     printf("%s for Fnordadel V%s\n", program, VERSION);
  238.  
  239.     lastmessage = 0L;
  240.     if (argc > 1 && argv[1][0] == '-') {
  241.     ++argv, --argc;
  242.     lastmessage = atol(1 + *argv);
  243.     }
  244.  
  245.     if (argc != 2)
  246.     crashout("usage: loadnet [-last-message] [net-list-file]");
  247.     if ((listing=fopen(argv[1],"r")) == NULL)
  248.     crashout("loadnet: cannot open `%s'", argv[1]);
  249.     fflush(stdin);
  250.     printf("\007WARNING!!! This program will wipe all* traffic pending on\
  251.  your network!!\nContinue (Y/N)? ");
  252.     c = getchar();
  253.     if (c != 'Y' && c != 'y') {
  254.     if (fromdesk())
  255.         hitkey();
  256.     exit(0);
  257.     }
  258.     if (readSysTab(YES)) {
  259.  
  260.     initnetBuf(&netBuf);
  261.     initroomBuf(&roomBuf);
  262.  
  263.     if (lastmessage < 0) {
  264.         lastmessage = cfg.newest + lastmessage;
  265.         if (lastmessage < cfg.oldest)
  266.         lastmessage = cfg.oldest;
  267.     }
  268.     else if (lastmessage == 0 || lastmessage > cfg.newest)
  269.         lastmessage = cfg.newest;
  270.  
  271.     ctdlfile(temp, cfg.netdir, "ctdlnet.sys");
  272.     drename(temp,"ctdlnet.tmp");
  273.     free(netTab);
  274.     cfg.netSize = 0;
  275.     netTab = malloc(0);
  276.     dunlink(temp);
  277.     if ((netfl = dcreat(temp)) < 0)
  278.         crashout("can't open netfile");
  279.     loadnodes(listing);
  280.     fclose(listing);
  281.     writeSysTab();
  282.     dclose(netfl);
  283.     }
  284.     if (fromdesk())
  285.     hitkey();
  286. }
  287.